// pages/pagegoods/pagegoods.js
import {
    getgoods
} from "../../request/api"
Page({

    /**
     * 页面的初始数据
     */
    data: {
        goodsList: [],
        carList: [],
    },
    // 添加购物车
    addcar() {
        let list = this.data.goodsList
        let arrs = []
        if (!wx.getStorageSync("goodsList")) {
            arrs.push({
                goods_id: list.goods_id,
                goods_name: list.goods_name,
                goods_price: list.goods_price,
                goods_big_logo:list.goods_big_logo,
                num: 1,
                flag: false,

            })
            wx.setStorageSync('goodsList', arrs)
        } else {
            let arr6 = wx.getStorageSync("goodsList")
            console.log(arr6);
            let flag = false
            arr6.forEach(item => {
                if (item.goods_id == list.goods_id) {
                    flag = true
                    item.num++
                }
            })
            if (!flag) {
                arr6.push({
                    goods_id: list.goods_id,
                    goods_name: list.goods_name,
                    goods_price: list.goods_price,
                    num: 1,
                    flag: false
                })
            }
            wx.setStorageSync('goodsList', arr6)
        }

    },
    //跳转购物车
    gocar() {
        wx.switchTab({
            url: '/pages/car/car',
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        //   console.log(15,options.id);
        let id = options.id
        getgoods(id).then(res => {
            console.log(19, res);
            this.setData({
                goodsList: res.data.message
            })
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})